package com.system.dao.impl;



import com.system.config.Jdbc;
import com.system.dao.UserData;
import com.system.domin.User;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDataMapper implements UserData {
    private final Jdbc jb;

    public UserDataMapper() {
        this.jb = new Jdbc();  // 初始化数据可加载类
    }

    @Override
    public void insert(User user) {
        try {
            String sql = "insert into user values('"+user.getEMAIL()+"','"+user.getPASSWORD()+"')";
            this.jb.getStmt().executeUpdate(sql);
        }catch (SQLException e)
        {
            throw new RuntimeException(e);
        }

    }

    @Override
    public boolean selectByEmailController(String email) {
        try {
            String sql = "select *  from user where email='"+email+"'";
            ResultSet rs = this.jb.getStmt().executeQuery(sql);
           if (rs.next()) return true;
        }catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean selectForLogin(String email, String password) {
        try {
            String sql = "select *  from user where email='"+email+"'";
            ResultSet rs = this.jb.getStmt().executeQuery(sql);
            if (rs.next())
            {
                return rs.getString("email").equals(email) && rs.getString("password").equals(password);
            }
        }catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        return false;
    }

}
